<?php

App::uses('AppModel', 'Model');

/**
 * Productsinstance Model
 *
 * @property Products $Products
 * @property Removalreasons $Removalreasons
 */
class Productsinstance extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'products_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        //'doc' => array(
           // 'date' => array(
               // 'rule' => array('date'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
           // ),
        //),
        'number_removed' => array(
            'removed-1' => array(
                'rule' => array('numeric'),
                'message' => 'Numbers only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'removed-2' => array(
                    'rule' => array('range', 0, 1000),
                    'message' => 'Please enter a number between 0 and 1000',
                
            ),
        ),
        'removalreasons_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
  function dateFormatAfterFind($dateString) {
        return date('d/m/Y', strtotime($dateString));
    }

    function afterFind($results, $primary = false) {
        foreach ($results as $key => $val) {
            
            if (isset($val['Productsinstance']['doc'])) {
                $results[$key]['Productsinstance']['doc'] = $this->dateFormatAfterFind($val['Productsinstance']['doc']);
            }
        }
        return $results;
    }

    function dateFormatBeforeSave($dateString) {
        $newDate = explode("/", $dateString);
        return $newDate[2] . "-" . $newDate[1] . "-" . $newDate[0];
    }

    function beforeSave($options = array()) {
        
        if (!empty($this->data['Productsinstance']['doc'])) {
            $this->data['Productsinstance']['doc'] = $this->dateFormatBeforeSave($this->data['Productsinstance']['doc']);
        }
        return true;
    }

    public $belongsTo = array(
        'Product' => array(
            'className' => 'Products',
            'foreignKey' => 'products_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Removalreason' => array(
            'className' => 'Removalreasons',
            'foreignKey' => 'removalReasons_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

}

